package spitfire.ksim.view;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import spitfire.ksim.node.KSensor;
import spitfire.ksim.node.KSensorNode;
import spitfire.ksim.simulator.KStateChangeListener;

public class KNodePanel extends JPanel {
	private static final long serialVersionUID = 3015540603125013427L;
	public KNodePanel(KSensorNode sNode, KNodeViewFrame frame) {
		super();
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		List<KSensor> sensorList = sNode.getSensorList();
		for (final KSensor sensor : sensorList) {
			JPanel wrapper = new JPanel();
			wrapper.setLayout(new BoxLayout(wrapper, BoxLayout.PAGE_AXIS));
			
			JPanel infoPanel = new JPanel();
			infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.LINE_AXIS));
			infoPanel.add(new JLabel("Description:"));
			final JTextField sensorSdTxtFld = new JTextField(sensor.getDescription(), 12);
			sensorSdTxtFld.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					sensor.setDescription(sensorSdTxtFld.getText());
				}
			});
			infoPanel.add(sensorSdTxtFld);
			
			JButton resetSdBtn = new JButton("Reset ");
			resetSdBtn.setPreferredSize(new Dimension(52, 12));
			resetSdBtn.setMargin(new Insets(1,2,1,2));
			resetSdBtn.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					sensor.setDescription("Unannotated");
				}
			});
			infoPanel.add(resetSdBtn);
			
			infoPanel.add(new JLabel("   Power"));
			final JCheckBox sensorStatusChckbx = new JCheckBox();
			sensorStatusChckbx.setSelected(sensor.isPowerOn());
			sensorStatusChckbx.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					sensor.setPowerOn(sensorStatusChckbx.isSelected());
				}
			});
			infoPanel.add(sensorStatusChckbx);
			wrapper.add(infoPanel);
			final KStateChangeListener l = new KStateChangeListener() {
				@Override
				public void stateChanged() {
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
							sensorSdTxtFld.setText(sensor.getDescription());
							sensorStatusChckbx.setSelected(sensor.isPowerOn());
						}
					});
				}
			};
			sensor.addStateChangeListener(l);
			frame.addInternalFrameListener(new InternalFrameAdapter() {
				@Override
				public void internalFrameClosed(InternalFrameEvent e) {
					sensor.removeStateChangeListener(l);
					super.internalFrameClosed(e);
				}
			});
			
			wrapper.add(new KSensorDataPanel(sNode, sensor, sNode.getSimulator()));
			wrapper.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createTitledBorder(sensor.getSensorType()),
					BorderFactory.createEmptyBorder(-8,0,0,0)));
			this.add(wrapper);
		}
	}
}
